package com.sudichina.sudichina.view.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.bean.User;
import com.sudichina.sudichina.utils.ActivityFinishUtils;
import com.sudichina.sudichina.utils.DESMD5Utils;
import com.sudichina.sudichina.utils.SpUtils;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.HelpActivity;
import com.sudichina.sudichina.view.activity.LineManageActivity;
import com.sudichina.sudichina.view.activity.about.AboutUsActivity;
import com.sudichina.sudichina.view.activity.dentification.CarManagerActivity;
import com.sudichina.sudichina.view.activity.dentification.IdcardManagerActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;
import com.sudichina.sudichina.view.activity.main.MainActivity;
import com.sudichina.sudichina.view.activity.orlder.MyOrlderActivity;
import com.sudichina.sudichina.view.activity.setting.SettingActivity;
import com.sudichina.sudichina.view.activity.wallet.RechargeActivity;
import com.sudichina.sudichina.view.activity.wallet.WalletActivity;
import com.sudichina.sudichina.view.customview.LoadDialog;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;


/**
 * Created by yangweiqiao on 2016/11/26.
 */

public class LoginActivity extends BaseActivity implements onRequestDataFinish {
    @BindView(R.id.frag_login_iv_back)
    ImageView privateFragLoginIvBack;
    @BindView(R.id.frag_login_tv_fast)
    TextView privateFragLoginTvHelp;
    @BindView(R.id.act_home_et_phone)
    EditText privateActHomeEtPhone;
    @BindView(R.id.act_login_et_pwd)
    EditText privateActLoginEtPwd;
    @BindView(R.id.act_login_tv_login)
    TextView privateActLoginTvLogin;
    @BindView(R.id.act_login_tv_forget)
    TextView privateActLoginTvForget;
    @BindView(R.id.act_login_tv_reg)
    TextView privateActLoginTvReg;

    public static LoginActivity context;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**加载布局*/
        setContentView(R.layout.activity_login);
        /**绑定控件*/
        ButterKnife.bind(this);
        context = this;
    }


    @OnClick({R.id.frag_login_iv_back, R.id.frag_login_tv_fast, R.id.act_login_tv_login, R.id.act_login_tv_forget, R.id.act_login_tv_reg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.frag_login_iv_back://左上角的返回键
//登陆界面返回键会直接退出应用
                Toastutils.showToast("退出应用");
                ActivityFinishUtils.finishAllActivity();
                finish();

                break;
            case R.id.frag_login_tv_fast://快捷登录
                /*
                这里注意的是 当我们进入快捷登录的时候
                可以选择返回登录界面  就是说打开快捷登录的界面  登录的界面不会消失 可以返回 当我们快捷登录成功后
                登录的界面会销毁
                 */


                startActivity(new Intent(this, QuickLoginActivity.class));


                break;
            case R.id.act_login_tv_login://登录按钮
                //检验用户的信息  进行登录操作
//                login();
                // TODO: 2017/3/23 0023
                login1();
                break;
            case R.id.act_login_tv_forget://忘记密码
                //跳转忘记密码的界面
                startActivity(new Intent(this, ForgetPswActivity.class));


                break;
            case R.id.act_login_tv_reg: //注册新账号
                //跳转注册界面

                startActivity(new Intent(this, RegisterActivity.class));


                break;


        }
    }


    public void login() {
        loadDialog = new LoadDialog(this, false, "正在登陆,请稍等...");
// TODO: 2017/3/20 0020  测试数据 
//        HashMap<String, Object> map1 = new HashMap<>();
//        map1.put("accountName", "13810916949");
//        map1.put("accountPassword", DESMD5Utils.MD5("123123",false));
//        Type type1 = new TypeToken<Result<User>>() {
//        }.getType();
//        NetUtils.requestData(Api.API_LOGIN, map1, this, type1, false);
//        return;
        // TODO: 2017/3/20 0020  真实数据 
        //获取控件数据
        final String strUserInput = privateActHomeEtPhone.getText().toString().trim();
        final String strPwd = privateActLoginEtPwd.getText().toString().trim();
        //数据正确性判断
        if (strUserInput.isEmpty()) {
            Toastutils.showToast("手机/会员名/邮箱不能为空!");

        } else if (strPwd.isEmpty()) {
            Toastutils.showToast("密码不能为空!");
        } else {
            //进行登陆
            //显示加载进度条
            loadDialog = new LoadDialog(this, false, "正在登陆,请稍等...");


            loadDialog.show();
//显示一个不可以点击外部被取消的对话框
            //开启网络请求
            //回调参数
            //判断用户是否登录
            //正确 回复 跳转界面到主界面
            //错误  提示用户的信息 是密码还是账号错误
            String md5_pwd = DESMD5Utils.MD5(strPwd, false);

            //保存用户名和密码
            SpUtils.putString(MyApp.getContext(), Contains.NAME, strUserInput);
            SpUtils.putString(MyApp.getContext(), Contains.PASSWORD, md5_pwd);

            HashMap<String, Object> map = new HashMap<>();
            map.put("accountName", strUserInput);
            map.put("accountPassword", md5_pwd);
            Type type = new TypeToken<Result<User>>() {
            }.getType();
            NetUtils.requestData(Api.API_LOGIN, map, this, type, false);

        }
    }

    public void login1() {
        loadDialog = new LoadDialog(this, false, "正在登陆,请稍等...");
// TODO: 2017/3/20 0020  测试数据
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("accountName", "13810916949");
        map1.put("accountPassword", DESMD5Utils.MD5("123123", false));
        Type type1 = new TypeToken<Result<User>>() {
        }.getType();
        //保存用户名和密码
        SpUtils.putString(MyApp.getContext(), Contains.NAME, "13810916949");
        SpUtils.putString(MyApp.getContext(), Contains.PASSWORD, DESMD5Utils.MD5("123123", false));
        NetUtils.requestData(Api.API_LOGIN, map1, this, type1, false);


    }

    LoadDialog loadDialog;

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        Result<User> bean1 = (Result<User>) bean;
        int code = bean1.getCode();

        User user = bean1.getData();


        String msg = bean1.getMsg();
        System.out.println("code: " + code + "data:" + user + "msg:" + msg);
        if (code == 8003) {
            Toastutils.showToast("用户名不存在");
            loadDialog.dismiss();
            return;
        }
        if (code == 8004) {
            Toastutils.showToast("密码错误");
            loadDialog.dismiss();
            return;
        }

        if (code == 1) {
            //保存为全局变量
            MyApp.setCurrentUser(user);

            /**
             * 登录成功之后设置别名
             */


            JPushInterface.setAlias(this, user.getAccountName(), null);

            switch (getIntent().getIntExtra(Contains.L, 0)) {

                //我的订单
                case Contains.MYORLDER_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, MyOrlderActivity.class);
                    }
                    break;
//我的钱包
                case Contains.MYWALL_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, WalletActivity.class);
                    }
                    break;
//油卡充值
                case Contains.YOUKA_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, RechargeActivity.class);
                    }
                    break;
//认证验证
                case Contains.RENZHENG_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, IdcardManagerActivity.class);
                    }

                    break;
//车辆管理
                case Contains.CARMANAGER_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, CarManagerActivity.class);
                    }
                    break;
//线路管理
                case Contains.LINEMANAGER_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, LineManageActivity.class);
                    }
                    break;
//帮助说明
                case Contains.HELP_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, HelpActivity.class);
                    }
                    break;
//关于我们
                case Contains.ABOUT_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, AboutUsActivity.class);
                    }
                    break;
//设置
                case Contains.SETTING_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, SettingActivity.class);
                    }
                    break;
//主界面
                case Contains.MAIN:
                    if (intent == null) {
                        intent = new Intent(this, MainActivity.class);
                    }
                    break;
                //主界面
                case Contains.QIANGDAN_EXTRA:
                    if (intent == null) {
                        intent = new Intent(this, MainActivity.class);


                    }
                    break;


            }


            startActivity(intent);

            loadDialog.dismiss();
            finish();
        }
    }

    @Override
    public void requestdataFailed(IOException e) {
        Toastutils.showToast("登录异常:" + e);
        loadDialog.dismiss();
    }

    public static void startMeAndInitIsLogin(Activity context, int extrs) {
        //判断当前的用户是否登录

        if (MyApp.getCurrentUser() == null) {
            //当前的用户没有登录
            //跳转到登录界面
            Intent intent = new Intent(context, LoginActivity.class);
            intent.putExtra(Contains.L, extrs);
            context.startActivity(intent);
            context.finish();


        }
    }


}
